
const { findDiary,addDiary,update} = require('../model/diaryModel')


//获取
const getDiaries=async(req,res,next)=>{
    let {id,date}=req.body;
    let obj={id};
    if(date!=='all'){
        obj.date=date
    }

    const result = await findDiary(obj);
    
    if (result.length||result.length===0) {
        res.send({ httpStatus: 200,  total: result.length, data: result })
    } else {
        res.send({ httpStatus: 400,  msg: '查询出错' })
    }
}
//新增
const writeDiary=async (req,res,next)=>{
    const {date,weather,content,id}=req.body;
    let obj={
        id,
        date,
        weather:weather?weather:'',
        content
    }
    const result=await addDiary(obj)
    if(result){
        res.send({httpStatus:200,msg:'增加成功',data:result})
    }else{
        res.send({httpStatus:400,msg:'增加出错'})
    }
}



//修改
const updateDiary = async (req, res) => {
    const { _id } = req.body;
    const query = { _id };
    const updated = req.body;
    const result = await update(query, updated)
    if (result.nModified !== 0) {
        res.send({ httpStatus: 200,  msg: '修改成功' })
    } else {
        res.send({ httpStatus: 400,  msg: '修改出错' })
    }

}
module.exports = {
    getDiaries,
    writeDiary,
    updateDiary
}